import Vue from 'vue'
import Router from 'vue-router'
import Login from '../pages/login/Login.vue'
import Home from '../pages/home/home.vue'
import Users from '../pages/users/users'

import { Message } from 'element-ui';
Vue.use(Router);


var rout=new Router({
  //mode: 'history',
    mode: 'hash',
  base: process.env.BASE_URL,
  routes: [
      {
          path: '/',
          name: 'home',
          component: Home,
          children:[
              {path:'/users',name:'users',component:Users,meta:{show:true}},
              {path:'/roles',name:'rolelist',component:()=>import('../pages/rolelist/rolelist.vue')},
              {path:'/rights',name:'rightlist',component:()=>import('../pages/rightlist/rightlist.vue')}
          ],
          redirect:'/users'
      },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (about.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
    }
  ]
});

rout.beforeEach((to,from,next)=>{
    console.log(to,'打印to路由信息');
    if(to.name === 'login'){
        //console.log('hhhh');
       //this.$message({type:'error',message:'您还没有登陆'});
        Message({type:'error',message:'请您有登陆哦，小乖乖'});
        //这样写message单独局部引用就可以，this.$message不可以。可能是因为不在组件内使用
        next()
    }else{
        var token=localStorage.getItem('token');
        //var token=this.localStorage.getItem('token');
        if(!token){
            //this.$message.error('您还没有登陆');
            //

            console.log('您还没有登陆，请先登陆');
            next({path:'/login'})
        }else{
            next()
        }


    }
});
export default rout;
